const PENDING = "pending";
const FULFILLED = "fulfilled";
const REJECTED = "rejected";

class MyPromise {
  constructor(executor) {
    executor(this.resolve, this.reject)
  }
  state = PENDING;  
  promiseResult = null;
  onFulfilledCB = [];
  onRejectedCB = [];
  resolve = (res) => {
    if (!this.state == PENDING) return;
    this.state = FULFILLED;
    this.promiseResult = res;
    while(this.onFulfilledCB.length) {
      this.onFulfilledCB.shift()(this.promiseResult);
    }
  }
  reject = () => {
    if (!this.state == PENDING) return;
    this.state = REJECTED;
    this.promiseResult = res;
    while(this.onRejectedCB.length) {
      this.onRejectedCB.shift()(this.promiseResult);
    }
  }
  then = (onFulfilled, onRejected) => {
    return new MyPromise((nextResolve, nextRejected) => {
      const bindPromise = (fn) => {

      }

      if (this.state == FULFILLED) {
        bindPromise(onFulfilled);
      } else if (this.state == REJECTED) {
        bindPromise(onRejected);
      } else if (this.state == PENDING) {
        this.onFulfilledCB.push(bindPromise(onFulfilled));
      }
    })
  }
}
